/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.logical.allhit.gui.action;

import com.logical.allhit.gui.MainFrom;
import com.logical.allhit.gui.MainView;
import com.logical.allhit.gui.model.AccountTableModel;
import java.awt.Event;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;

/**
 *
 * @author Sanjeet Jha
 */
public class StartAction extends AbstractAction{
    
    private MainFrom mainFrom;

    public StartAction(MainFrom mainFrom) {
        putValue(NAME, bundle.getString("start.name"));
        putValue(SHORT_DESCRIPTION, bundle.getString("start.shortdis"));
        putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_S, Event.CTRL_MASK));
        this.mainFrom = mainFrom;
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        boolean[] selection = mainFrom.getSelection();
        try{
            if(!selection[0] && !selection[1] && !selection[2]){
                JOptionPane.showMessageDialog(MainView.getInstance().getFram(), "select at list one option ");
                return;
            }
            if(getSelectedCount() <= 0){
                JOptionPane.showMessageDialog(MainView.getInstance().getFram(), "select at list one Account in account table ");
                return;
            }
            setEnabled(false);
//            Task task = new Task(selection, this, mainFrom);
            TaskNew task = new TaskNew(selection, this, mainFrom, mainFrom.getDay(), mainFrom.getToDay(), mainFrom.isFormLast());
            task.addPropertyChangeListener(mainFrom);
            task.execute();
        }catch(Exception ex){
            Logger.getLogger(StartAction.class.getName()).log(Level.WARNING, ex.getMessage(), ex);
        }
    }
    
    private int getSelectedCount(){
        AccountTableModel model = mainFrom.getAccountTableModel();
        int selected = 0;
        for(int i=0 ; i< model.getRowCount(); i++){
            if((Boolean)model.getValueAt(i, 0)){
                selected++;
            }
        }
        return selected;
    }
    
}
